package com.dianrui.fmi.activity;

import android.content.Intent;
import android.widget.ListView;
import android.widget.TextView;

import com.dianrui.fmi.Constant;
import com.dianrui.fmi.R;
import com.dianrui.fmi.adapter.HelpListAdapter;
import com.dianrui.fmi.bean.HelpList;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.Url;
import com.dianrui.fmi.view.pulltoRefresh.PullToRefreshBase;
import com.dianrui.fmi.view.pulltoRefresh.PullToRefreshListView;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Help
 */

public class HelpActivity extends BaseActivity {
    private int mPageIndex=1;
    private List<HelpList> cateHelpListAll = new ArrayList<>();
    private HelpListAdapter helpListAdapter;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.listview)
    PullToRefreshListView listview;

    @Override
    public int getContentViewId() {
        return R.layout.activity_help;
    }

    @Override
    public void initView() {
        title.setText(getString(R.string.help_txt));
        getExplainList(false);
        helpListAdapter = new HelpListAdapter(HelpActivity.this, cateHelpListAll);
        listview.setAdapter(helpListAdapter);
        helpListAdapter.setOnHelpClickListener(new HelpListAdapter.HelpClickAdapter() {
            @Override
            public void onClick(String explainid) {
                startActivity(new Intent(HelpActivity.this, WebviewActivity.class).putExtra("explainId",explainid));
            }
        });
        listview.setMode(PullToRefreshBase.Mode.BOTH);
        listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                getExplainList(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mPageIndex++;
                getExplainList(false);
            }
        });

    }

    private void getExplainList(final boolean isRfresh) {
        if (isRfresh) {
            mPageIndex = 1;
        }
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("p", mPageIndex);
        OkHttpUtils.getInstance().postWithMemberToken(Url.EXPLAIN, jsonObject.toString(),  new OkHttpUtils.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    if (listview != null) {
                        listview.onRefreshComplete();
                    }
                    if (isRfresh) {
                        cateHelpListAll.clear();
                    }
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject &&!isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                if (null != jsonArray &&jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        HelpList helpList = new HelpList();
                                        helpList.explain_id = jsonArray.optJSONObject(i).optString("explain_id");
                                        helpList.title = jsonArray.optJSONObject(i).optString("title");
                                        cateHelpListAll.add(helpList);
                                    }
                                    if (helpListAdapter != null) {
                                        helpListAdapter.refresh(cateHelpListAll);
                                    }
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }


}
